//
//  TravelEnorllController.m
//  lv678
//
//  Created by apple on 15/8/30.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  旅游报名

#import "TravelEnorllController.h"
#import "AFNetworking.h"
#import "EnrollLoginController.h"
#import "DLRadioButton.h"
#import "ValidateHelper.h"
#import "CalendarHomeViewController.h"
@interface TravelEnorllController ()<EnrollLoginControllerDelegate>
{
    CalendarHomeViewController *chvc;
    NSString *temp;
}

@property (weak, nonatomic) IBOutlet UIButton *pushToLast;//返回上一个视图
@property (weak, nonatomic) IBOutlet UITextField *userName;//用户名
@property (weak, nonatomic) IBOutlet UITextField *travelName;//出行人
@property (weak, nonatomic) IBOutlet UITextField *identityCard;//身份证号
@property (weak, nonatomic) IBOutlet UITextField *contactNum;//联系电话
@property (weak, nonatomic) IBOutlet UITextField *travelRoad;//出行路线
@property (weak, nonatomic) IBOutlet UIButton *offDate;//出发日期
@property (weak, nonatomic) IBOutlet UITextField *offNo;//出发班次
@property (weak, nonatomic) IBOutlet UITextField *backNo;//返回班次
@property (weak, nonatomic) IBOutlet UIButton *backTime;//返回时间

@property (weak, nonatomic) IBOutlet UILabel *lblSubmit;//提交按钮label
@property (nonatomic, copy)NSString *offTripTypes;//出发交通工具
@property (nonatomic, copy)NSString *backTripTypes;//返回交通工具
- (IBAction)offTools:(DLRadioButton *)sender;//出发交通工具单选按钮
- (IBAction)backTools:(DLRadioButton *)sender;//返回交通工具单选按钮

@end

@implementation TravelEnorllController
@synthesize userName,travelName,identityCard,contactNum,travelRoad,offDate,offNo,backNo,backTime,offTripTypes,backTripTypes;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.pushToLast addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [self initView];
}
#pragma mark - 初始化视图
- (void)initView{
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(submit)];
    recognizer.numberOfTapsRequired = 1;
    [self.lblSubmit addGestureRecognizer:recognizer];
    self.userName.delegate = self;
    self.travelName.delegate = self;
    self.identityCard.delegate = self;
    self.contactNum.delegate = self;
    self.travelRoad.delegate = self;
    self.offNo.delegate = self;
    self.backNo.delegate = self;
    [self.offDate addTarget:self action:@selector(offDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.backTime addTarget:self action:@selector(offDateBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    EnrollLoginController *login = [[UIStoryboard storyboardWithName:@"Main" bundle:nil]instantiateViewControllerWithIdentifier:@"enrollLogin"];
    login.delegate = self;
}

#pragma mark - 点击空白处关闭软键盘
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

#pragma mark - 点击软键盘return按钮关闭软键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return YES;
}

#pragma mark - 选择出发时间
- (void)offDateBtn:(UIButton *)btn{
        chvc = [[CalendarHomeViewController alloc]init];
    [chvc setTrainToDay:365 ToDateforString:nil];
    
        chvc.calendarblock = ^(CalendarDayModel *model){
            
            if (model.holiday) {
                
                [btn setTitle:[NSString stringWithFormat:@"%@",[model toString]] forState:UIControlStateNormal];
                
            }else{
                
                [btn setTitle:[NSString stringWithFormat:@"%@ ",[model toString]] forState:UIControlStateNormal];
                
            }
        };
        
        [self.navigationController pushViewController:chvc animated:YES];
}

#pragma mark - 实现代理接收数据
- (void)sendValue:(NSString *)textField{
    temp = textField;
}
#pragma mark - 提交按钮请求接口
- (void)submit{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *timpDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSString *url = [timpDict objectForKey:@"tripapplyapp"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer  = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    offTripTypes = [self judgeTripTypes:self.offTripTypes];
    backTripTypes = [self judgeTripTypes:self.backTripTypes];
    BOOL falg = [self validateTextField];
    if (falg) {
        NSDictionary *data = @{@"serviceName":@"addApplyInfo",@"queryParameters":@{@"applyId":temp,@"username":userName.text,@"tripuser":travelName.text,@"identityNo":identityCard.text,@"telephone":contactNum.text,@"triproad":travelRoad.text,@"starttime":offDate.titleLabel.text,@"arrive_traffic":offTripTypes,@"arrive_ban":offNo.text,@"endtime":backTime.titleLabel.text,@"back_traffic":backTripTypes,@"back_ban":backNo.text}};
        [manager POST:url parameters:data success:^(AFHTTPRequestOperation *operation, id responseObject) {
            //防止再次提交
            self.lblSubmit.userInteractionEnabled = NO;
            self.lblSubmit.alpha = 0.4;
            int status = [[responseObject objectForKey:@"status"]intValue];
            if (status == 1) {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"报名成功，确定点击返回" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alertView show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
    }
    
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [alertView didMoveToSuperview];
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}
#pragma mark -判断出行工具是飞机还是火车
- (NSString *)judgeTripTypes:(NSString *)tripTypes{
    if ([offTripTypes isEqualToString:@"飞机"]) {
        tripTypes = @"1";
    }else{
        tripTypes = @"2";
    }
    return tripTypes;
}

#pragma mark - 验证输入框
- (BOOL)validateTextField{
    BOOL falg = YES;
    ValidateHelper *validate = [[ValidateHelper alloc]init];
    BOOL phoneFalg = [validate isMobileNumber:self.contactNum.text];
    NSString *off = offDate.titleLabel.text;
    NSString *back = backTime.titleLabel.text;
    if (userName.text.length == 0) {
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        falg = NO;
        [userName becomeFirstResponder];
    }else{
        if (travelName.text.length == 0) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"出行人不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertView show];
            falg = NO;
            [travelName becomeFirstResponder];
        }else{
            if (identityCard.text.length == 0) {
                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"身份证号不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alertView show];
                falg = NO;
                [identityCard becomeFirstResponder];
            }else{
                if (contactNum.text.length == 0) {
                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"手机号不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alertView show];
                    falg = NO;
                    [contactNum becomeFirstResponder];
                }else{
                    if (!phoneFalg) {
                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确手机号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                        [alertView show];
                        falg = NO;
                        [contactNum becomeFirstResponder];
                    }else{
                        if (travelRoad.text.length == 0) {
                            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"出行线路不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                            [alertView show];
                            falg = NO;
                            [travelRoad becomeFirstResponder];
                        }else{
                            if ([off isEqualToString:@"请选择出发时间"]) {
                                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"出发时间不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                [alertView show];
                                falg = NO;
                                [travelRoad resignFirstResponder];
                            }else{
                                if (offNo.text.length == 0) {
                                    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"出发班次不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                        [alertView show];
                                    falg = NO;
                                    [offNo becomeFirstResponder];
                                }else{
                                    if ([back isEqualToString:@"请选择返回时间"]) {
                                        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"请选择正确返回时间" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                        [alertView show];
                                        falg = NO;
                                        [self.view resignFirstResponder];
                                        
                                    }else{
                                            if (backNo.text.length == 0) {
                                                UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:@"返回班次不能为空" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                                                [alertView show];
                                                falg = NO;
                                                [backTime becomeFirstResponder];
                                                                                        }
                                        }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return falg;
}

#pragma mark - 返回根视图
- (void)back{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 设置出行交通工具参数
- (IBAction)offTools:(DLRadioButton *)sender {
    self.offTripTypes =  sender.selectedButton.titleLabel.text;
}

#pragma mark -设置返回交通工具参数
- (IBAction)backTools:(DLRadioButton *)sender {
    self.backTripTypes =  sender.selectedButton.titleLabel.text;

}
@end
